﻿using EWSEditor.CRM.Forms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EWSEditor.CRM
{
    class DiagnosticCheck
    {
        public string CheckName;
        public string Symptom ="";
        public string Cause = "";
        public string Resolution = "";
        public string TechnicalDetails = "";
        public string DelegatedMethod;
        public string DelegatedFixItMethod;

        public string DynamicHelpUrl;
        public DiagnosticCheckResult? DiagnosticResult;
        public DiagnosticType DiagnosticCheckType;
        public bool ShowFixIt = false;
        public bool IsSupportViewable;
        public bool TestFinished = false;
        public bool ShowDynamicHelp;

        public string OrgSettingUseCase;
        public string OrgSettingCurrentVal;

        public DiagnosticForm DiagForm;

        public DiagnosticCheck(string Checkname, string Delegatedmethod, string Delegatedfixitmethod, string Dynamichelpurl, DiagnosticType DiagType, bool Showfixit, bool Issupportviewable, bool Showdynamichelp, DiagnosticForm Diagform)
        {
            CheckName = Checkname;
            DelegatedMethod = Delegatedmethod;
            DelegatedFixItMethod = Delegatedfixitmethod;
            DynamicHelpUrl = Dynamichelpurl;
            DiagnosticCheckType = DiagType;
            ShowFixIt = Showfixit;
            IsSupportViewable = Issupportviewable;
            ShowDynamicHelp = Showdynamichelp;
            DiagForm = Diagform;
          
        }

        public void RunCheck()
        {
            try
            {
                if (null == DelegatedMethod)
                {
                    throw new Exception("DelegatedMethod was not passed to the DiagnosticCheck Initializer.");
                }

                BackgroundWorker bgWorker = new BackgroundWorker();
                bgWorker.DoWork += (sender, e) =>
                {
                    CallMethod(DelegatedMethod);
                };

                bgWorker.RunWorkerCompleted += (sender, e) =>
                {
                    CheckProperties();
                };

                bgWorker.RunWorkerAsync();

            }
            catch (Exception ex)
            {
                StringBuilder sb = new StringBuilder("DiagnosticCheck Failed: ");
                sb.Append(this.CheckName);
                sb.AppendLine(ex.Message);
                sb.AppendLine(ex.StackTrace);
                DiagnosticForm.ShowMessage(sb.ToString(), "DiagnosticCheck Has Failed", this.DiagForm);
            }
        }

        /// <summary>
        /// Ensures that all DiagnosticChecks have required properties and throws an exception if one is missing.
        /// </summary>
        private void CheckProperties()
        {
            if (this.DiagnosticResult == null)
            {
                throw new Exception("DiagnosticResult is null.");
            }

            if (this.TestFinished == false)
            {
                throw new Exception("TestFinished is set to false.");
            }

            if (this.Symptom == null)
            {
                throw new Exception("Symptom is null.");
            }

            if (this.Cause == null)
            {
                throw new Exception("Cause is null.");
            }

            if (this.Resolution == null)
            {
                throw new Exception("Resolution is null.");
            }
        }

        public void CallMethod(string DelegateMethod)
        {
            var type = Type.GetType("EWSEditor.CRM.DiagnosticCheckMethods");

            if (type != null)
            {
                var method = type.GetMethod(DelegateMethod);

                if (method != null)
                {
                    method.Invoke(null, new object[] {this});
                }

            }

        }
    }


    enum DiagnosticCheckResult
    {
        Success,
        Warning,
        Error,
        Information
    }

    enum DiagnosticType
    {
        Configuration,
        IncomingEmail,
        ACT,
        OrgDbOrgSetting
    }
}
